package forum.communication;

import forum.server.controller.ForumFacadeInt;
import forum.server.domain.PostContent;
import forum.server.exceptions.*;

/**
 * @author Tamir 
 *
 */
public class ModifyMessageMessage extends ClientMessage {

	private static final long serialVersionUID = -4738980852130566587L;
	
	/**
	 * The id of the message which the client wants to modify.
	 * (The forum is nested).
	 */
	private int messageId;
	
	/**
	 * The new content of the message.
	 */
	private PostContent content;

	public ModifyMessageMessage(int messageId, PostContent content) {
		this.messageId = messageId;
		this.content = content;
	}

	/* (non-Javadoc)
	 * @see forum.tcpcommunicationlayer.ClientMessage#doOperation(forum.server.domainlayer.ForumFacade)
	 */
	@Override
	public ServerResponse doOperation(ForumFacadeInt forum) {
		try {
			forum.modifyPost(this.messageId, content);
			return new ServerResponse("The post #"+this.messageId+" was successfully modified", true);
		} catch (UnauthorizedOpException e) {
			return new ServerResponse(e.getMessage(), false);
		} catch (NotLoggedInException e) {
			return new ServerResponse("Guests cannot modify posts. Please login.", false);
		} catch (NoSuchPostException e) {
			return new ServerResponse(e.getMessage(), false);
		} catch (DatabaseException e) {
			return new ServerResponse("The server is currently down for maintanance.\nPlease try again later.",false); 
		}	
	}

}
